*** Convert SPSS to Stata using R script first

* set working directory
cd "D:\Box\Box Sync\Research\Data\Australia\for Dataverse"

* load raw file (converted from SPSS)
clear
use US_raw

* Rename zygosity variable
rename ZYG mz

* Delineate twin pairs
rename COMPID id
gen twinid=string(id)
gen twinnumber = regexs(0) if(regexm(twinid, "[0-9][0-9]$"))
gen twinpair= regexs(0) if(regexm(twinid, "[0-9][0-9][0-9][0-9][0-9]"))
drop twinid
destring twinnumber, replace
destring twinpair, replace
egen pairnumber=group(twinpair)

* Identify complete pairs
gen completepair=.
qui sum pairnumber
local temp=r(max)
foreach num of numlist 1/`temp' {
qui sum twinpair if pairnumber==`num'
qui replace completepair=r(N) if pairnumber==`num'
}
replace completepair=completepair-1


/* List of variables needed
Social ideology (no religion)
Religiosity
Big Five and Facets
	Agreeableness
	Agreeableness (Altruism)
	Agreeableness (Compliance)
	Conscientiousness
	Conscientiousness (Order)
	Conscientiousness (Self-discipline)
	Extraversion
	Extraversion (Activity)
	Extraversion (Assertiveness)
	Neuroticism
	Neuroticism (Anxiety)
	Neuroticism (Depression)
	Openness
	Openness (Aesthetics)
	Openness (Ideas)
Income
Education
Age
Sex
*/

*** Social ideology
** Code with higher as conservative
* Conservative items 1, 5, 7, 12, 13, 14, 16, 17, 18, 21, 22, 24, 26, 27
* Here 3 was coded as disagree, 2 as uncertain, 1 as agree
* For strength, 3 was coded as not strongly, 2 as strongly, 1 as very strongly
foreach num of numlist 1/27 {
replace Qq19_A_`num'=Qq19_A_`num'-2
}

foreach num of numlist 1 5 7 12 13 14 16 17 18 21 22 24 26 27 {
replace Qq19_A_`num'=Qq19_A_`num'*-1
}

foreach num of numlist 1/27 {
recode Qq20_A_`num' 1=3 2=2 3=1
}

foreach num of numlist 1/27 {
gen wpcomp`num'_2008=Qq19_A_`num'*Qq20_A_`num'
replace wpcomp`num'_2008=. if Qq19_A_`num'==. | Qq20_A_`num' ==.
}

** Calculate social ideology
/* Items in social ideology
4 - Pornography
6 - Women's equality
7 - Death penalty
8 - Premarital sex
9 - Gay marriage
10 - Abortion rights
11 - Evolution
16 - Protect gun rights
25 - Stem cell research
26 - Abstinence-only sex education*/

#delimit ;
gen wpscale_socialnorelig_2008 = (wpcomp4_2008 + wpcomp6_2008 + 
wpcomp7_2008 + wpcomp8_2008 + wpcomp9_2008 + 
wpcomp10_2008 + wpcomp11_2008 + 
wpcomp16_2008 + wpcomp25_2008 + wpcomp26_2008)/10;

#delimit ;
alpha wpcomp4_2008 wpcomp6_2008 wpcomp7_2008 wpcomp8_2008 wpcomp9_2008 
	wpcomp10_2008 wpcomp11_2008 wpcomp16_2008 wpcomp25_2008 wpcomp26_2008;
#delimit cr

*** Religiosity
gen attend = Qq80
recode attend 1=6 2=5 3=4 4=3 5=2 6=1

gen importance = Qq81
recode importance 1=4 2=3 3=2 4=1

gen relig_combined = ((attend-1)/5 + (importance - 1)/3) /2

* Descriptive stats on religious denomination
decode Qq82b_1, gen(religion_other)
gen catholic = (Qq82==2)
replace catholic = 1 if religion_other=="Catholic" | religion_other=="PROTESTANT/CATHOLIC" | religion_other=="PROTESTANT/CATHOLIC/JEWISH"
gen protestant = (Qq82==1)
#delimit ;
replace protestant = 1 if 
	religion_other=="AFL" |
	religion_other=="alc/christian" |
	religion_other=="APOSTOLIC" |
	religion_other=="Assembly of God-Pentecostal" |
	religion_other=="Attend Church of Religious Science" |
	religion_other=="baptist" |
	religion_other=="Baptist" |
	religion_other=="BAPTIST" |
	religion_other=="christian" |
	religion_other=="Christian" |
	religion_other=="CHRISTIAN" |
	religion_other=="Christian (non-denominational)" |
	religion_other=="CHRISTIAN CHURCH" |
	religion_other=="Christian/non-denominational" |
	religion_other=="Episcopalean" |
	religion_other=="episcopalean" |
	religion_other=="Episcopalian" |
	religion_other=="evangelical" |
	religion_other=="Evangelical Christian" |
	religion_other=="Jehovah's Witness" |
	religion_other=="Jehovah Witness" |
	religion_other=="lutheran-missouri synod" |
	religion_other=="lutheran" |
	religion_other=="lUTHERAN" |
	religion_other=="Lutheran" |
	religion_other=="LUTHERAN" |
	religion_other=="Lutheran / wisconsin synod" |
	religion_other=="lutheren" |
	religion_other=="luthern" |
	religion_other=="Luthern" |
	religion_other=="LUTHERN" |
	religion_other=="luthern/adventist" |
	religion_other=="LUTHREN" |
	religion_other=="methodist" |
	religion_other=="Methodist" |
	religion_other=="METHODIST" |
	religion_other=="MORMON" |
	religion_other=="New Testament Church" |
	religion_other=="NON-DENOMINATIONAL, Evangelical" |
	religion_other=="Non-denomination" |
	religion_other=="non-denominational" |
	religion_other=="NON-DENOMINATIONAL" |
	religion_other=="Non-denominational; Bible based; spirit" |
	religion_other=="Non denominational-bible based" |
	religion_other=="NON DENOMINATIONAL" |
	religion_other=="nondenominational" |
	religion_other=="Nondenominational" |
	religion_other=="Pentecostal Evangelical" |
	religion_other=="PROTESTANT/CATHOLIC" |
	religion_other=="PROTESTANT/CATHOLIC/JEWISH" |
	religion_other=="Quaker" |
	religion_other=="Quaker or Buddhist" |
	religion_other=="sda" |
	religion_other=="Spirit Filled Christian - Lutheran" |
	religion_other=="Unitarian-Universalist" |
	religion_other=="unitarian" |
	religion_other=="Unitarian" |
	religion_other=="UNITED METHODIST" |
	religion_other=="unity";
#delimit cr


*** Big Five Traits and Facets
*Traits

gen b5_ex=0
gen b5_ag=0
gen b5_co=0
gen b5_ne=0
gen b5_op=0

replace Qq21_A_31=. if Qq21_A_31==0

foreach var in 1 11 16 26 36 {
replace b5_ex=b5_ex+Qq21_A_`var'
}
foreach var in 6 21 31 {
replace b5_ex=b5_ex+(6-Qq21_A_`var')
}

foreach var in 7 17 22 32 42 {
replace b5_ag=b5_ag+Qq21_A_`var'
}
foreach var in 2 12 27 37 {
replace b5_ag=b5_ag+(6-Qq21_A_`var')
}

foreach var in 3 13 28 33 38 {
replace b5_co=b5_co+Qq21_A_`var'
}
foreach var in 8 18 23 43 {
replace b5_co=b5_co+(6-Qq21_A_`var')
}

foreach var in 4 14 19 29 39 {
replace b5_ne=b5_ne+Qq21_A_`var'
}
foreach var in 9 24 34 {
replace b5_ne=b5_ne+(6-Qq21_A_`var')
}

foreach var in 5 10 15 20 25 30 40 44 {
replace b5_op=b5_op+Qq21_A_`var'
}
foreach var in 35 41 {
replace b5_op=b5_op+(6-Qq21_A_`var')
}

replace b5_op=b5_op/10
replace b5_op=(b5_op-1)/4

replace b5_co=b5_co/9
replace b5_co=(b5_co-1)/4

replace b5_ex=b5_ex/8
replace b5_ex=(b5_ex-1)/4

replace b5_ag=b5_ag/9
replace b5_ag=(b5_ag-1)/4

replace b5_ne=b5_ne/8
replace b5_ne=(b5_ne-1)/4

label var b5_ex "Big Five - Extraversion"
label var b5_ag "Big Five - Agreeableness"
label var b5_co "Big Five - Conscientiousness"
label var b5_ne "Big Five - Neuroticism"
label var b5_op "Big Five - Openness"

* Facets - coded according to Appendix B of Soto and John 2009 Journal of Research in Personality

gen b5_ex_assertiveness=0
gen b5_ex_activity=0
gen b5_ag_altruism=0
gen b5_ag_compliance=0
gen b5_co_order=0
gen b5_co_selfdiscipline=0
gen b5_ne_anxiety=0
gen b5_ne_depression=0
gen b5_op_aesthetics=0
gen b5_op_ideas=0


foreach var in 1 26 {
replace b5_ex_assertiveness=b5_ex_assertiveness+Qq21_A_`var'
}

foreach var in 6 21 31 {
replace b5_ex_assertiveness=b5_ex_assertiveness+(6-Qq21_A_`var')
}

foreach var in 11 16 {
replace b5_ex_activity=b5_ex_activity+Qq21_A_`var'
}
/* 36 excluded */





foreach var in 7 22 32 {
replace b5_ag_altruism=b5_ag_altruism+Qq21_A_`var'
}
foreach var in 27 {
replace b5_ag_altruism=b5_ag_altruism+(6-Qq21_A_`var')
}

foreach var in 17 {
replace b5_ag_compliance=b5_ag_compliance+Qq21_A_`var'
}
foreach var in 2 12 {
replace b5_ag_compliance=b5_ag_compliance+(6-Qq21_A_`var')
}
/* excluded 37 and 42 */




foreach var in 8 18 {
replace b5_co_order=b5_co_order+(6-Qq21_A_`var')
}

foreach var in 13 28 38 {
replace b5_co_selfdiscipline=b5_co_selfdiscipline+Qq21_A_`var'
}
foreach var in 23 43 {
replace b5_co_selfdiscipline=b5_co_selfdiscipline+(6-Qq21_A_`var')
}
/* excluded 03 33 */




foreach var in 19 39 {
replace b5_ne_anxiety=b5_ne_anxiety+Qq21_A_`var'
}
foreach var in 9 34 {
replace b5_ne_anxiety=b5_ne_anxiety+(6-Qq21_A_`var')
}

foreach var in 4 29 {
replace b5_ne_depression=b5_ne_depression+Qq21_A_`var'
}
/* excluded 14 24 */



foreach var in 30 44 {
replace b5_op_aesthetics=b5_op_aesthetics+Qq21_A_`var'
}
foreach var in 41 {
replace b5_op_aesthetics=b5_op_aesthetics+(6-Qq21_A_`var')
}
foreach var in 10 15 25 40 {
replace b5_op_ideas=b5_op_ideas+Qq21_A_`var'
}
foreach var in 35 {
replace b5_op_ideas=b5_op_ideas+(6-Qq21_A_`var')
}
/* excluded 05 20 25 */



replace b5_op_aesthetics=b5_op_aesthetics/3
replace b5_op_aesthetics=(b5_op_aesthetics-1)/4

replace b5_op_ideas=b5_op_ideas/5
replace b5_op_ideas=(b5_op_ideas-1)/4

replace b5_co_order=b5_co_order/2
replace b5_co_order=(b5_co_order-1)/4

replace b5_co_selfdiscipline=b5_co_selfdiscipline/5
replace b5_co_selfdiscipline=(b5_co_selfdiscipline-1)/4

replace b5_ex_assertiveness=b5_ex_assertiveness/5
replace b5_ex_assertiveness=(b5_ex_assertiveness-1)/4

replace b5_ex_activity=b5_ex_activity/2
replace b5_ex_activity=(b5_ex_activity-1)/4

replace b5_ag_altruism=b5_ag_altruism/4
replace b5_ag_altruism=(b5_ag_altruism-1)/4

replace b5_ag_compliance=b5_ag_compliance/3
replace b5_ag_compliance=(b5_ag_compliance-1)/4

replace b5_ne_anxiety=b5_ne_anxiety/4
replace b5_ne_anxiety=(b5_ne_anxiety-1)/4

replace b5_ne_depression=b5_ne_depression/2
replace b5_ne_depression=(b5_ne_depression-1)/4



*** Income

rename Qq94 income



*** Education

rename Qq90 education



*** Age - check analayses to correct age

gen age=int(id/100000)
replace age=age+1900
replace age=2008-age



*** Sex
tab rsex




*** Keep needed variables
keep twinnumber completepair wpscale_socialnorelig_2008 relig_combined catholic protestant b5* income education age rsex mz pairnumber

order _all, alpha
order age rsex mz pairnumber twinnumber completepair, last


*** Scale 0 to 1

replace wpscale_socialnorelig_2008 = (wpscale_socialnorelig_2008+3)/6
replace income=(income-1)/6
replace education=(education-1)/6

save "US_cleaned.dta", replace 





*** Combine twins onto rows

recode twinnumber 0=1 1=2

foreach var of varlist b5_ag-age {
	rename `var' `var'_
}

reshape wide b5_ag_-age_, i(pairnumber) j(twinnumber)

save "US_cleaned_pairs.dta", replace
outsheet using "US_cleaned_pairs.csv", comma nolabel replace




*** Catholic and Protestant only datasets for supplemental analysis

preserve
* Catholic only
drop if catholic_1!=1 & catholic_2!=1 
outsheet using "US_cleaned_pairs_Catholic.csv", comma nolabel replace
restore

preserve
* Protestant only
drop if protestant_1!=1 & protestant_2!=1 
outsheet using "US_cleaned_pairs_Protestant.csv", comma nolabel replace
restore
